package com.cooper.moviedb.scraper;

import com.cooper.java.scraper.*;
import com.cooper.moviedb.bo.MediaEntity;
import com.cooper.moviedb.bo.MediaInfoEntity;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: cooperlee
 * Date: 11/15/12
 * Time: 9:11 AM
 * To change this template use File | Settings | File Templates.
 */
public class FlvcdScraper extends Scraper {

    private final String BROWSE_URL_TAG = "CreateBrowseUrl";
    private final String SEARCH_URL_TAG = "CreateSearchUrl";
    private final String BROWSE_MOVIE_TAG = "BrowseMovie";
    private final String BROWSE_PAGE_COUNT_TAG = "GetBrowsePageCount";
    private final String SEARCH_PAGE_COUNT_TAG = "GetSearchPageCount";
    private final String SEARCH_MOVIE_TAG = "SearchMovie";
    private final String MOVIE_DETAISL_TAG = "GetDetails";
    private final String BROWSE_REDIRECT_MOVIE_TAG = "BrowseRedirectMovie";
    private final String SEARCH_REDIRECT_MOVIE_TAG = "SearchRedirectMovie";
    private final String BROWSE_REDIRECT_PAGE_COUNT_TAG = "BrowseRedirectPageCount";
    private final String SEARCH_REDIRECT_PAGE_COUNT_TAG = "SearchRedirectPageCount";


    public FlvcdScraper(File scraperFile,DownloadParams downloadParams) {
        super(scraperFile,downloadParams);
    }


    public ResultUrl createBrowseUrl(int pageNumber) throws DownloadException, ScraperException {
        setBufferValue(1, String.valueOf(pageNumber));
        try {
            DefineTag tag = getDefineTag(BROWSE_URL_TAG);

            return ResultUrl.urlFactory(tag.matchRegExp(), tag.getEncodeType())
                    .get(0);
        } catch (DownloadException e) {
            throw e;
        } catch (Exception e) {
            e.printStackTrace();
            throw new ScraperException(BROWSE_URL_TAG, e);
        }
    }

    private static final  int FLVCD_SOURCE = 1;

    private static final int AUTO_TYPE = 1;

    private List<MediaEntity> anlyMedia(String axml,String encodeType){
        String xml = "<root>" + axml + "</root>";
        List<MediaEntity> result = new ArrayList<MediaEntity>();

        SAXReader reader = new SAXReader();
        Document document = null;
        try {
            document = reader.read(new ByteArrayInputStream(xml.getBytes()));
        } catch (DocumentException e) {
            e.printStackTrace();
            return result;

        }
        Element root = document.getRootElement();

        for (java.util.Iterator i = root.elementIterator("movie"); i.hasNext(); ) {
            Element foo = (Element) i.next();

            MediaEntity media = new MediaEntity();
            media.setDetailsPageEncode(encodeType);
            media.setId(foo.element("id").getText());
            media.setSource(FLVCD_SOURCE);
            media.setType(AUTO_TYPE);
            media.setDeleted(false);
            media.setRoot(true);

            for (java.util.Iterator si = foo.elementIterator();si.hasNext();){
                Element foo2 = (Element)si.next();
                media.getMediaInfoEntitySet().add(new MediaInfoEntity(media, foo2.getName(),foo2.getText()));
            }
            result.add(media);
        }

        return result;
    }

    public List<MediaEntity> extractBrowseResource(String page)
            throws DownloadException, ScraperException {
        setBufferValue(1, page);
        try {
            DefineTag tag = getDefineTag(BROWSE_MOVIE_TAG);


            return anlyMedia(tag.matchRegExp(),tag.getEncodeType());
            //return OnlineMovie.onlineMovieFactory(tag.matchRegExp(),
            //        scraperResId, tag.getEncodeType());
       // } catch (DownloadException e) {
       //     throw e;
        } catch (Exception e) {
            throw new ScraperException(BROWSE_MOVIE_TAG, e);
        }
    }


    public int getBrowsePageCount(String page) throws DownloadException,
            ScraperException {
        setBufferValue(1, page);
        try {
            String result = getDefineTag(BROWSE_PAGE_COUNT_TAG).matchRegExp();
            if ((result == null) || "".equals(result)) {
                return 0;
            } else {
                return Integer.parseInt(result);
            }
        } catch (DownloadException e) {
            throw e;
        } catch (Exception e) {
            throw new ScraperException(BROWSE_PAGE_COUNT_TAG, e);
        }
    }


    public String extractDetails(String page) throws DownloadException,
            ScraperException {
        setBufferValue(1, page);
        try {
            return getDefineTag(MOVIE_DETAISL_TAG).matchRegExp();
        } catch (DownloadException e) {
            throw e;
        } catch (Exception e) {
            throw new ScraperException(BROWSE_PAGE_COUNT_TAG, e);
        }
    }

}
